﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinkCard.Utils;
using LinkCard.Model;

namespace LinkCard.ShopCP
{
    public partial class ChangePassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            error.Visible = false;
        }

        private void ChangePass()
        {
            var sShopCp = Session["SHOPADMIN"].ToString();
            var sOldPasss = this.oldPass.Value.Trim();
            var sNewPass = this.newPassword.Value.Trim();

            var db = new TopLinkEntities();
            
            Int64 _id = 0;
            //0: đại lý, 1: shopcp
            if (sShopCp == "0")
            {
                _id = Convert.ToInt64(Session["ShopStaffID"].ToString());
                var _shopStaff = db.ShopStaffs.Where(t => t.UserID == _id).FirstOrDefault();
                if (_shopStaff != null)
                {
                    if (_shopStaff.Password == sOldPasss)
                    {
                        _shopStaff.Password = sNewPass;

                        db.SaveChanges();

                        Response.Redirect("/message_box.aspx?msg=7");
                    }
                    else
                    {
                        error.Visible = true;
                    }
                }
            }
            else
            {
                _id = Convert.ToInt64(Session["USER"].ToString()); //SupplierID
                var _supplier = db.Supplier.Where(t => t.SupplierID == _id).FirstOrDefault();
                if (_supplier != null)
                {
                    if (_supplier.Password == sOldPasss)
                    {
                        _supplier.Password = sNewPass;

                        db.SaveChanges();

                        Response.Redirect("/message_box.aspx?msg=7");
                    }
                    else
                    {
                        error.Visible = true;
                    }
                }
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            ChangePass();
        }
    }
}